package com.campus.secondhand.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.campus.secondhand.entity.Conversations;

/**
 * 会话Mapper接口
 */
public interface ConversationMapper extends BaseMapper<Conversations> {

        /**
         * 查找用户的所有会话
         * 
         * @param userId 用户ID
         * @return 会话列表
         */
        @Select("SELECT * FROM conversations WHERE user_a_id = #{userId} OR user_b_id = #{userId} ORDER BY updated_at DESC")
        List<Conversations> findUserConversations(@Param("userId") Long userId);

        /**
         * 查找用户的所有会话（分页）
         * 
         * @param userId   用户ID
         * @param offset   偏移量
         * @param pageSize 每页数量
         * @return 会话列表
         */
        @Select("SELECT * FROM conversations WHERE user_a_id = #{userId} OR user_b_id = #{userId} ORDER BY updated_at DESC LIMIT #{offset}, #{pageSize}")
        List<Conversations> findUserConversationsPaged(@Param("userId") Long userId, @Param("offset") Integer offset,
                                                       @Param("pageSize") Integer pageSize);

        /**
         * 查找两个用户之间的会话
         * 
         * @param userAId 用户A ID
         * @param userBId 用户B ID
         * @return 会话对象，若不存在返回null
         */
        @Select("SELECT * FROM conversations WHERE (user_a_id = #{userAId} AND user_b_id = #{userBId}) OR (user_a_id = #{userBId} AND user_b_id = #{userAId}) LIMIT 1")
        Conversations findConversationBetweenUsers(@Param("userAId") Long userAId, @Param("userBId") Long userBId);

        /**
         * 查找两个用户关于特定商品的会话
         * 
         * @param userAId   用户A ID
         * @param userBId   用户B ID
         * @param productId 商品ID
         * @return 会话对象，若不存在返回null
         */
        @Select("SELECT * FROM conversations WHERE ((user_a_id = #{userAId} AND user_b_id = #{userBId}) OR (user_a_id = #{userBId} AND user_b_id = #{userAId})) AND product_id = #{productId} LIMIT 1")
        Conversations findConversationBetweenUsersForProduct(@Param("userAId") Long userAId,
                                                             @Param("userBId") Long userBId,
                                                             @Param("productId") Long productId);

        /**
         * 更新会话的最后一条消息信息
         * 
         * @param id            会话ID
         * @param lastMessageId 最后一条消息ID
         * @param lastMessage   最后一条消息内容
         * @return 更新的记录数
         */
        @Update("UPDATE conversations SET last_message_id = #{lastMessageId}, last_message = #{lastMessage}, updated_at = NOW() WHERE id = #{id}")
        int updateLastMessage(@Param("id") Long id, @Param("lastMessageId") Long lastMessageId,
                        @Param("lastMessage") String lastMessage);

        /**
         * 重置用户在会话中的未读消息数
         * 
         * @param id     会话ID
         * @param userId 用户ID
         * @return 更新的记录数
         */
        @Update("UPDATE conversations SET user_a_unread_count = CASE WHEN user_a_id = #{userId} THEN 0 ELSE user_a_unread_count END, user_b_unread_count = CASE WHEN user_b_id = #{userId} THEN 0 ELSE user_b_unread_count END WHERE id = #{id}")
        int resetUserUnreadCount(@Param("id") Long id, @Param("userId") Long userId);

        /**
         * 增加用户在会话中的未读消息数
         * 
         * @param id     会话ID
         * @param userId 接收消息的用户ID
         * @return 更新的记录数
         */
        @Update("UPDATE conversations SET user_a_unread_count = CASE WHEN user_a_id = #{userId} THEN user_a_unread_count + 1 ELSE user_a_unread_count END, user_b_unread_count = CASE WHEN user_b_id = #{userId} THEN user_b_unread_count + 1 ELSE user_b_unread_count END WHERE id = #{id}")
        int incrementUserUnreadCount(@Param("id") Long id, @Param("userId") Long userId);

        /**
         * 获取用户所有会话的未读消息总数
         * 
         * @param userId 用户ID
         * @return 未读消息总数
         */
        @Select("SELECT SUM(CASE WHEN user_a_id = #{userId} THEN user_a_unread_count ELSE user_b_unread_count END) FROM conversations WHERE user_a_id = #{userId} OR user_b_id = #{userId}")
        int countUserTotalUnreadMessages(@Param("userId") Long userId);
}